package org.osami.commons.dialog.tuidisplayservice;

import org.osami.commons.dialog.IDisplayService;
import java.io.*;

public class TUIDisplayService implements IDisplayService {

	@Override
	public int display(String text, String[] options) {
		
		if (options.length > 0) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
			
			System.out.println(text);
			for (int i = 0; i < options.length; i++) {
				System.out.println((i + 1) + ": " + options[i]);
			}
			
			int res = 0;
			
			do {
				System.out.print("Selection > ");
				try {
					String input = reader.readLine();
					res = Integer.parseInt(input);
				} catch (Exception ex) {
					System.out.println(ex.toString());
				}
			} while (res < 1 || res > options.length);
			
			return res - 1;
				
			
		} else {
			System.out.println(text);
		}
		
		return 0;
	}

	
	
}
